<template>
  <div class="todo-wrapper">
    <div class="table-tool" @click="handleAdd">
      <i class="el-icon-plus icon"></i>
      <span class="title">添加</span>
    </div>
    <el-table :data="tableData" style="width: 100%" height="450" stripe border>
      <el-table-column type="index" width="80" label="序号"> </el-table-column>
      <el-table-column prop="name" label="名称" width="180"> </el-table-column>
      <el-table-column
        prop="content"
        label="内容"
        min-width="180"
      ></el-table-column>
      <el-table-column prop="deadlineTime" label="截止时间" width="160">
      </el-table-column>
      <el-table-column
        label="状态"
        width="90"
        :filters="[
          { text: '待办', value: 0 },
          { text: '已完成', value: 1 },
          { text: '已删除', value: -1 },
        ]"
        :filter-method="filterTag"
        filter-placement="bottom-end"
      >
        <template slot-scope="{ row }">
          <el-tag v-if="row.status === 0">待办</el-tag>
          <el-tag type="success" v-else-if="row.status === 1">已完成</el-tag>
          <el-tag type="warning" v-else-if="row.status === -1">已删除</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160">
        <template slot-scope="{ row }">
          <el-button
            type="text"
            class="tool-btn"
            :disabled="row.status !== 0"
            @click.stop="handleEdit(row)"
            >编辑</el-button
          >
          <el-button
            type="text"
            class="tool-btn"
            :disabled="row.status !== 0"
            @click.stop="handleChangeStatus(row, 1)"
            >完成</el-button
          >
          <el-button
            type="text"
            class="tool-btn"
            :disabled="row.status === -1"
            @click.stop="handleChangeStatus(row, -1)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      :current-page.sync="currentPage"
      :page-size="10"
      layout="total, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" size="small">
      <el-form
        :model="form"
        label-position="top"
        :rules="rules"
        ref="form"
        label-width="100px"
        class="form"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="form.content"></el-input>
        </el-form-item>
        <el-form-item label="截止时间" prop="deadline">
          <el-date-picker
            type="datetime"
            placeholder="选择截止时间"
            v-model="form.deadline"
            style="width: 100%"
            value-format="yyyy-MM-dd HH:mm:SS"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSave">保 存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "Todo",
  data() {
    return {
      currentPage: 1,
      total: 0,
      dialogTitle: "添加",
      dialogVisible: false,
      form: {},
      rules: {},
      tableData: [],
    };
  },
  mounted() {
    this.query();
  },
  methods: {
    handleAdd() {
      this.dialogTitle = "添加";
      this.dialogVisible = true;
      this.form = {};
    },
    filterTag(value, row) {
      return row.status === value;
    },
    handleChangeStatus(row, status) {
      row.status = status;
      const url = "/todo-web/update";
      axios.post(url, { ...row }).then(() => {
        this.query();
      });
    },
    handleEdit(row) {
      this.dialogTitle = "编辑";
      this.form = Object.assign({ deadline: row.deadlineTime }, row);
      this.dialogVisible = true;
    },
    query() {
      const url = "/todo-web/list";
      axios.get(url).then((res) => {
        let data = res.data;
        if (data) {
          this.tableData = res.data.data;
          this.total = data.total;
        }
      });
    },
    handleSave() {
      const url = this.form.id ? "/todo-web/modify" : "/todo-web/create";
      let params = {
        ...this.form,
      };
      axios.post(url, params).then(() => {
        this.query();
        this.dialogVisible = false;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.todo-wrapper {
  width: 1250px;
  height: 100%;
  padding-top: 100px;
  padding-left: 200px;
  box-sizing: border-box;
  .table-tool {
    width: 80px;
    height: 32px;
    margin-bottom: 10px;
    line-height: 32px;
    text-align: center;
    cursor: pointer;
    .title {
      display: inline-block;
      margin-left: 5px;
    }
  }
  .tool-btn:not(:last-child)::after {
    content: "|";
    color: rgba(151, 151, 151, 0.26);
    margin-left: 8px;
  }
  .pagination {
    width: 100%;
    margin-top: 5px;
    text-align: right;
    padding-right: 50px;
    box-sizing: border-box;
  }
}
.icon {
  font-size: 18px;
}
</style>
